<template>
	<div class="app-container">
		<!-- 搜索 -->
		<div class="filter-container tw-filter">
      <!-- <el-button class="filter-item tw-filter-button" size="medium" icon="el-icon-edit" v-has="$store.state.user.btn_common+'add'" @click="handleAdd()">增减</el-button>
      <el-button class="filter-item tw-filter-button" size="medium" icon="el-icon-delete" v-has="$store.state.user.btn_common+'del'" @click="handleDelete()">批量删除</el-button> -->

			<el-input v-model="queryList.member_id" placeholder="用户ID" class="filter-item tw-filter-input" clearable />
			<el-input v-model="queryList.member_name" placeholder="用户名" class="filter-item tw-filter-input" clearable />
			<el-select v-model="queryList.operate_type" class="filter-item tw-filter-input" placeholder="选择增减积分" clearable>
				<el-option key="" label="全部" value="" />
				<el-option key="1" label="增加积分" value="1" />
				<el-option key="2" label="减少积分" value="2" />
			</el-select>
      <el-date-picker @change="changeData" value-format="yyyy-MM-dd" v-model="add_time_scope" type="daterange" unlink-panels :picker-options="pickerOptions"
      start-placeholder="开始日期" end-placeholder="结束日期" class="filter-item tw-filter-input-mr" clearable></el-date-picker>

			<el-button v-waves class="filter-item tw-filter-button" size="medium" icon="el-icon-search" @click="handleFilter">搜索</el-button>
		</div>

		<!-- 列表 -->
    <el-table
      v-loading="listLoading"
      :data="tableList"
      @selection-change="handleSelectionChange"
      border fit highlight-current-row style="width: 100%;"
    >
      <el-table-column type="selection" width="50" />
			<el-table-column label="用户ID" prop="member_id" align="center" />
      <el-table-column label="用户名" prop="member_name" align="left" />
			<!-- <el-table-column label="用户姓名" prop="member_real_name" align="left" /> -->
			<el-table-column label="积分值" prop="points" align="left" />
			<el-table-column label="操作阶段" prop="type_name" align="left" />
			<el-table-column label="操作描述" prop="desc" align="left" />
      <el-table-column label="操作时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.add_time | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
			<!-- <el-table-column label="操作人" prop="admin_real_name" align="center" /> -->
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
      	<template slot-scope="{row}">
      		<el-button size="mini" v-has="$store.state.user.btn_common+'del'" @click="handleDelete(row.id)">删除</el-button>
      	</template>
      </el-table-column> -->
    </el-table>
    <pagination v-show="isPage && listTotal > 0" :total="listTotal" :page.sync="queryList.curpage" :limit.sync="queryList.page" @pagination="getList" />

		<!-- 添加/编辑 -->
    <el-dialog width="45%" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form label-position="right" label-width="120px" ref="dataForm" :rules="rules" :model="form">
    		<el-form-item label="用户ID" prop="member_id">
    			<el-input v-model="form.member_id" placeholder="请输入用户ID"/>
    		</el-form-item>
        <el-form-item label="增减类型" prop="operate_type">
        	<el-radio-group v-model="form.operate_type">
        		<el-radio :label="1">增加积分</el-radio>
        		<el-radio :label="2">减少积分</el-radio>
        	</el-radio-group>
        </el-form-item>
        <el-form-item label="积分值" prop="points">
        	<el-input v-model="form.points" />
        </el-form-item>
    		<el-form-item label="描述" prop="desc">
    		  <el-input v-model="form.desc" type="textarea" :rows="5" maxlength="100" show-word-limit placeholder="请输入描述" />
    		</el-form-item>
    	</el-form>
    	<div slot="footer" class="dialog-footer">
    		<el-button @click="dialogStatus==='create'?createData():updateData()">保存</el-button>
    		<el-button @click="dialogFormVisible = false">取消</el-button>
    	</div>
    </el-dialog>

	</div>
</template>

<script>
	import Pagination from '@/components/Pagination' // 基于el分页的二次封装
	import waves from '@/directive/waves' // waves directive
	export default {
		components: { Pagination },
		directives: { waves },
		data() {
			return {
				// 数据列表
				listLoading: true,
				listTotal: 0,
				isPage: false,
				tableList: [],
				multipleSelection: [],//多选数据
        // 添加/编辑弹窗
        dialogFormVisible: false,
        dialogStatus: '',
        textMap: {
        	update: '编辑',
        	create: '添加'
        },
        //键字段
        keyName: 'id',
        apiPath: 'member.points',

        // 搜索数据
        queryList: {
        	page: this.$store.state.settings.page, // 每页显示数量
        	curpage: 1, // 当前页码
        	title: '', //  标题
        	type: '', //显示类型ID
					member_id:'',	//用户ID
					member_name:'',	//用户名
          operate_type: null,
					add_time_scope:'', //时间范围
        },
        add_time_scope: [],

				// 表单数据
				form: {
					member_id: '', //用户ID
					operate_type: 1, //增减类型 1-增积分 2-减积分
					points: '', //积分值
					desc: '', //描述
				},
				// 重置数据
				formReset: {
					member_id: '', //用户ID
					operate_type: 1, //增减类型 1-增积分 2-减积分
					points: '', //积分值
					desc: '', //描述
				},
        // 验证
        rules: {
        	member_id: [{ required: true, message: '请输入用户ID！', trigger: 'change' }],
        	operate_type: [{ required: true, message: '请选择积分增减类型！', trigger: 'blur' }],
        	points: [{ required: true, message: '请输入积分值！', trigger: 'change' }],
        },

        //带快捷选项时间
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        }

			}
		},
		created() {
			//获取数据列表
			this.getList()
		},
		methods: {
      //选择搜索时间范围
      changeData(val) {
      	this.queryList.add_time_scope = val[0] + ' - ' + val[1];
      },
/*******************************公共方法******************************/
      // 搜索
      handleFilter() {
      	this.queryList.curpage = 1
      	this.tableList = []
        this.listTotal = 0
      	this.getList()
      },
      // 列表选择
      handleSelectionChange(val) {
      	this.multipleSelection = val
      },
      // 重置form
      resetForm() {
      	this.form = JSON.parse(JSON.stringify(this.formReset))
      },
			// 获取数据列表
			getList() {
				this.listLoading = true
				this.$api.fetchList(this.queryList, this.apiPath).then(response => {
					this.tableList = response.list
					this.listTotal = response.list_count
					this.isPage = response.is_page
          this.listLoading = false
				})
			},
			// 添加
			handleAdd() {
			  this.resetForm()
			  this.dialogStatus = 'create'
			  this.dialogFormVisible = true
			  this.$nextTick(() => {
			    this.$refs['dataForm'].clearValidate()
			  })
			},
      // 添加-数据提交
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$api.add(this.form, this.apiPath).then(() => {
              // 重新获取列表
              this.queryList.curpage = 1
              this.tableList = []
              this.getList()

              this.dialogFormVisible = false
              this.$message({
                message: '添加成功',
                type: 'success'
              })
            })
          }
        })
      },
      // 删除
      handleDelete(id = 0) {
        let current = this
        let id_str = ''
        if (!id) {
          // 多选
          if (!this.multipleSelection.length) {
            this.$message({
              message: '请选择删除的条目！',
              type: 'warning'
            })
            return false
          }
          // 数据处理
          this.multipleSelection.forEach(item => {
            id_str += item[current.keyName] + ','
          })
        } else {
          id_str = id
        }

        // 提交数据
        this.$confirm('确认删除数据吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$api.del({ id_str: id_str }, this.apiPath).then(response => {
            // 重新获取列表
            this.getList()
            this.$message({
              message: '删除成功',
              type: 'success'
            })
          })
        })
      },
/*******************************公共方法END******************************/
		}
	}
</script>
